package com.niuxiaofa.newlaw.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.entity.LocalMedia;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.GlideUtils;
import com.niuxiaofa.newlaw.Tools.UIUtils;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.annotations.NonNull;

public class SelectImageAdapter extends RecyclerView.Adapter<SelectImageAdapter.MyViewHolder> {


    private List<LocalMedia> mData;
    private int mCountLimit = 3;

    int selectPosition = -1;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onTakePhotoClick();

        void onItemLongClick(View view, int position);

        void onDeleteItemClick(int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public SelectImageAdapter(List<LocalMedia> data, boolean type) {
        this.mData = data;
        if (type) {
            mCountLimit = 1;
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_img, parent, false);

        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;


    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {


        if (selectPosition == position) {

            holder.delete_img.setVisibility(View.VISIBLE);
            holder.shade.setVisibility(View.VISIBLE);
        } else {
            holder.delete_img.setVisibility(View.GONE);
            holder.shade.setVisibility(View.GONE);
        }


        if (position == getItemCount() - 1 && mData.size() < mCountLimit) {

            holder.add_img.setImageBitmap(UIUtils.getBitmap(R.mipmap.add_img));

            holder.add_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onTakePhotoClick();
                }
            });
        } else {

            // 本地图片

            Log.e("path", mData.get(position).getPath());

            if (mData.get(position).getPath().indexOf("http") != -1) {
                GlideUtils.loadRoundedImage(UIUtils.getContext(), mData.get(position).getPath(), holder.add_img);
            } else {
                File file = new File(mData.get(position).getPath());
                GlideUtils.loadSDCardImg(UIUtils.getActivity(), file, holder.add_img);
            }


            // 网络图片
            // Glide.with(BaseApplication.getInstance().getApplicationContext()).load(mData.get(position)).into(holder.imageView);
            holder.add_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击操作，后续可添加点击后的响应
                    selectPosition = position;

                    //  selectPosition = position;
                    Log.e("selectPosition", selectPosition + "");
                    notifyDataSetChanged();
                }
            });


            holder.delete_img.setOnClickListener(e -> {


                selectPosition = -10086;

                mOnItemClickListener.onDeleteItemClick(position);


            });


            // 长按监听
            holder.add_img.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onItemLongClick(v, position);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        // 满 9张图就不让其添加新图
        if (mData != null && mData.size() >= mCountLimit) {
            return mCountLimit;
        } else {
            return mData == null ? 1 : mData.size() + 1;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.add_img)
        ImageView add_img;

        @BindView(R.id.delete_img)
        ImageView delete_img;


        @BindView(R.id.shade)
        View shade;

        private MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}